const livereload = require('livereload');
// const rollupConfig = require('../system.rollup.config');
const path = require('path');
const nodemon = require('nodemon');
var chokidar = require('chokidar');
var { compilerStyle } = require('./src/style');
const fs = require('fs');

// 启动 livereload 服务器
const lrserver = livereload.createServer();
lrserver.watch(path.join(__dirname, '../src/theme/system'));

chokidar
  .watch(['./src/theme/system/lib/style/singlePage.scss', './src/theme/system/lib/style/**/*.scss', './src/theme/system/templates/ordersinglepage_index.liquid', './src/theme/system/snippets/step_**.liquid'], {
    ignoreInitial: true,
  })
  .on('change', () => {
    if (fs.existsSync('./src/theme/system/lib/style/singlePage.scss')) {
      var to = './src/theme/system/assets/singlePage.css';
      compilerStyle(
        {
          purge: ['./src/theme/system/templates/ordersinglepage_index.liquid', './src/theme/system/snippets/step_**.liquid'],
          from: './src/theme/system/lib/style/singlePage.scss',
          to: to,
        },
        'system',
        true,
        function () {
          console.log('编译完成');
          lrserver.refresh('*');
        }
      );
    }
  });
chokidar
  .watch(['./src/theme/system/lib/style/cod.scss', './src/theme/system/lib/style/**/*.scss', './src/theme/system/templates/ordercodpage_index.liquid', './src/theme/system/snippets/cod_**.liquid'], {
    ignoreInitial: true,
  })
  .on('change', () => {
    if (fs.existsSync('./src/theme/system/lib/style/cod.scss')) {
      var to = './src/theme/system/assets/cod.css';
      compilerStyle(
        {
          purge: ['./src/theme/system/templates/ordercodpage_index.liquid', './src/theme/system/snippets/cod_**.liquid', './src/theme/system/snippets/step_**.liquid'],
          from: './src/theme/system/lib/style/cod.scss',
          to: to,
        },
        'system',
        true,
        function () {
          console.log('编译完成');
          lrserver.refresh('*');
        }
      );
    }
  });

nodemon({
  script: 'app/app.js',
  ignore: ['node_modules/', 'oemsaas/', 'src/', 'config/', '.git/', 'admin/'],
}).on('start', function () {});
